//
//  ImageListView.m
//  Castlery
//
//  Created by xie weizhi on 8/9/17.
//  Copyright © 2017 weizhi. All rights reserved.
//

#import "ImageListView.h"
#import "ImageListCollectionViewCell.h"
#import <YYKit.h>

static NSString *const CollectionViewCellID = @"cellID";

@interface ImageListView () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, copy, readwrite) NSArray <NSURL *> *urls;
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation ImageListView

- (void)reloadWithUrls:(NSArray <NSURL *> *)urls {
    _urls = urls;
    _selectedIndex = 0;
    
    [self.collectionView reloadData];
}

- (instancetype)init {
    if (self = [super init]) {
        [self configureViews];
    }
    
    return self;
}

- (void)configureViews {
    [self addSubview:self.collectionView];

    self.collectionView.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint
     activateConstraints:@[[_collectionView.topAnchor constraintEqualToAnchor:self.topAnchor],
                           [_collectionView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
                           [_collectionView.leftAnchor constraintEqualToAnchor:self.leftAnchor],
                           [_collectionView.rightAnchor constraintEqualToAnchor:self.rightAnchor],]];
}

#pragma mark - Collection Delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.urls.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ImageListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellID
                                                                           forIndexPath:indexPath];
    
    [cell setSelected:(_selectedIndex == indexPath.row)];
    
    [cell.imageView setImageWithURL:self.urls[indexPath.row] options:YYWebImageOptionProgressive];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    _selectedIndex = indexPath.row;
    
    [collectionView reloadData];

    if ([self.delegate respondsToSelector:@selector(imageList:didSelectImageAtIndex:)]) {
        [self.delegate imageList:self didSelectImageAtIndex:_selectedIndex];
    }
}

#pragma mark - Accessors

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake(ProductListItemSize, ProductListItemSize);
        layout.minimumInteritemSpacing = 20.f;
        layout.sectionInset = UIEdgeInsetsMake(0.f, 20.f, 0.f, 0.f);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[ImageListCollectionViewCell class]
            forCellWithReuseIdentifier:CollectionViewCellID];
    }
    
    return _collectionView;
}

@end
